﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GMap.NET;
using GMap.NET.WindowsPresentation;
using gmapWPFTesting.CustomMarkers;

namespace gmapWPFTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // marker
        //GMapMarker currentMarker;

        PointLatLng previousPosition = new PointLatLng(-36.866667, 174.766667);
        PointLatLng currentPosition = new PointLatLng(-36.866667, 174.766667);
        bool storyIsBeingEntered = false;
        string currentStoryType = "";

        public MainWindow()
        {
            InitializeComponent();

            // map events
            //MainMap.OnCurrentPositionChanged += new CurrentPositionChanged(MainMap_OnCurrentPositionChanged);
            //MainMap.OnTileLoadComplete += new TileLoadComplete(MainMap_OnTileLoadComplete);
            //MainMap.OnTileLoadStart += new TileLoadStart(MainMap_OnTileLoadStart);
            //MainMap.OnMapTypeChanged += new MapTypeChanged(MainMap_OnMapTypeChanged);
            MainMap.MouseMove += new System.Windows.Input.MouseEventHandler(MainMap_MouseMove);
            MainMap.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(MainMap_MouseLeftButtonDown);
            //MainMap.Loaded += new RoutedEventHandler(MainMap_Loaded);
            //MainMap.MouseEnter += new MouseEventHandler(MainMap_MouseEnter);
        }

        void MainMap_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            System.Windows.Point p = e.GetPosition(MainMap);
            currentPosition = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);
        }

        // move current marker with left holding
        void MainMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                System.Windows.Point p = e.GetPosition(MainMap);
                currentPosition = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //auckland position
            PointLatLng aucklandPosition = new PointLatLng(-36.866667, 174.766667);

            // config map
            //MainMap.MapType = MapType.GoogleMap;
            //MainMap.MapType = MapType.OpenStreetMap;
            MainMap.AllowDrop = true;
            MainMap.MaxZoom = 20;
            MainMap.MinZoom = 10;
            MainMap.Zoom = MainMap.MinZoom + 1;
            MainMap.Position = aucklandPosition;
            MainMap.BoundsOfMap = new RectLatLng(aucklandPosition.Lat, aucklandPosition.Lng, 0.1504, 0.2282);

            //MessageBox.Show(MainMap.CacheLocation.ToString());
        }


        //see if a icon/symbol/category is dropped onto the map
        private void MainMap_Drop(object sender, DragEventArgs e)
        {
            System.Windows.Point p = e.GetPosition(MainMap);

            currentPosition = MainMap.FromLocalToLatLng((int)p.X, (int)p.Y);
        }

        /*
         * determine if the object being dragged onto the map area is supported 
            for the drop functionality
         */
        private void MainMap_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.All;
        }

        //house image click
        private void houseIcon_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragDrop.DoDragDrop(this, this, DragDropEffects.All);
            currentStoryType = "House";
            inputStory();
        }

        //romance image click
        private void romanceIcon_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragDrop.DoDragDrop(this, this, DragDropEffects.All);
            currentStoryType = "Romance";
            inputStory();
        }

        //restaurant image click
        private void restaurantIcon_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragDrop.DoDragDrop(this, this, DragDropEffects.All);
            currentStoryType = "Restaurant";
            inputStory();
        }

        private void inputStory()
        {
            //only proceed if a story is not already being entered
            if (!storyIsBeingEntered)
            {

                //check to make sure the new story/memory position is a NEW position
                if (currentPosition.Lat != previousPosition.Lat && currentPosition.Lng != previousPosition.Lng)
                {
                    //populate the input canvas image with apporpriate 'memory icon' image
                    if(currentStoryType.Equals("House"))
                    {
                        memoryIconImage.Source = new BitmapImage(new Uri("/gmapWPFTesting;component/category_2_icon.png", UriKind.Relative)); 
                    }
                    else if(currentStoryType.Equals("Romance"))
                    {
                         memoryIconImage.Source = new BitmapImage(new Uri("/gmapWPFTesting;component/category_1_icon.png", UriKind.Relative));
                    }
                    else if(currentStoryType.Equals("Restaurant"))
                    {
                         memoryIconImage.Source = new BitmapImage(new Uri("/gmapWPFTesting;component/category_3_icon.png", UriKind.Relative));
                    }
                    else
                    {
                         memoryIconImage.Source = new BitmapImage(new Uri("/gmapWPFTesting;component/keyboard4.png", UriKind.Relative));
                    }
                    

                    //make the input prompt visible
                    storyInputCanvas.Visibility = System.Windows.Visibility.Visible;
                    storyInputTextBox.Focus();
                    storyIsBeingEntered = true;
                }
            }
            
        }

        private void submitStoryEntry_Click(object sender, RoutedEventArgs e)
        {
            //if the input is empty..
            if (storyInputTextBox.Text.Equals(""))
            {
                MessageBox.Show("Please input something for the story");
            }
            else
            {
                GMapMarker m = new GMapMarker(currentPosition);
                {
                    Placemark p = null;

                    string ToolTipText;
                    if (p != null)
                    {
                        ToolTipText = p.Address;
                    }
                    else
                    {
                        ToolTipText = currentPosition.ToString();
                    }
                }
                m.ZIndex = 55;

                previousPosition = currentPosition;

                //add the marker to the map markers collection based on which type of marker it is
                if (currentStoryType.Equals("House"))
                {
                    m.Shape = new homeMarker(this, m, storyInputTextBox.Text);

                }

                if (currentStoryType.Equals("Romance"))
                {
                    m.Shape = new romanceMarker(this, m, storyInputTextBox.Text);

                }

                if (currentStoryType.Equals("Restaurant"))
                {
                    m.Shape = new restaurantMarker(this, m, storyInputTextBox.Text);

                }

                MainMap.Markers.Add(m);

                storyInputCanvas.Visibility = System.Windows.Visibility.Hidden;
                storyInputTextBox.Text = "";
                storyIsBeingEntered = false;
            }
        }
    }
}
